<?php 
session_start();
function converturl($str) {
    
    
    $remove="~ ` ! @ # $ % ^ & * ( ) _ + | \\ = ' \" ; : ? / > . , < ] [ { }";
    $from   = "à á ạ ả ã â ầ ấ ậ ẩ ẫ ă ằ ắ ặ ẳ ẵ è é ẹ ẻ ẽ ê ề ế ệ ể ễ ì í ị ỉ ĩ ò ó ọ ỏ õ ô ồ ố ộ ổ ỗ ơ ờ ớ ợ ở ỡ ù ú ụ ủ ũ ư ừ ứ ự ử ữ ỳ ý ỵ ỷ ỹ đ ";
    $to     = "a a a a a a a a a a a a a a a a a e e e e e e e e e e e i i i i i o o o o o o o o o o o o o o o o o u u u u u u u u u u u y y y y y d ";
    $from  .= " À Á Ạ Ả Ã Â Ầ Ấ Ậ Ẩ Ẫ Ă Ằ Ắ Ặ Ẳ Ẵ È É Ẹ Ẻ Ẽ Ê Ề Ế Ệ Ể Ễ Ì Í Ị Ỉ Ĩ Ò Ó Ọ Ỏ Õ Ô Ồ Ố Ộ Ổ Ỗ Ơ Ờ Ớ Ợ Ở Ỡ Ù Ú Ụ Ủ Ũ Ư Ừ Ứ Ự Ử Ữ Ỳ Ý Ỵ Ỷ Ỹ Đ ";
    $to    .= " a a a a a a a a a a a a a a a a a e e e e e e e e e e e i i i i i o o o o o o o o o o o o o o o o o u u u u u u u u u u u y y y y y d ";
    $remove =  explode(" ", $remove);
    $from =  explode(" ", $from);
    $to =  explode(" ", $to);
    $str = str_replace($from,$to, $str);
    $str = str_replace($remove,"", $str);
    $str=strip_tags($str);
    $str = iconv("UTF-8","ISO-8859-1//TRANSLIT//IGNORE",$str);
    //$str = iconv("ISO-8859-1","UTF-8",$str);
    $str= str_replace(" ","-", $str);
    $str=  strtolower($str);
    return $str;
    
}
class region extends CI_Controller  {

        /**
        * Index Page for this controller.
        *
        * Maps to the following URL
        * 		http://example.com/index.php/welcome
        *	- or -  
        * 		http://example.com/index.php/welcome/index
        *	- or -
        * Since this controller is set as the default controller in 
        * config/routes.php, it's displayed at http://example.com/
        *
        * So any other public methods not prefixed with an underscore will
        * map to /index.php/welcome/<method_name>
        * @see http://codeigniter.com/user_guide/general/urls.html
        */
       private $_configs = null;
       function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->library('javascript');
            $this->load->library('session');
            $this->load->library('smarty3','','smarty');
            $this->_configs["title"]="Quản Lý Khu Vực";
            $this->_configs["authority"]="region";
            $this->_configs["MinAuthority"]=0;
            $this->load->model('admin-planners/region_model','region_model');
            if($this->checkauthority()<0){AccessDenied();}
            
            
        }
        public function checkauthority(){
            if(isset($_SESSION["ADP-USER"]["AUTHORITY"])){
                $myau=-1;
                if(is_array($_SESSION["ADP-USER"]["AUTHORITY"])){
                    foreach ($_SESSION["ADP-USER"]["AUTHORITY"] as $au){
                        if($au["keyword"]=="view" && $myau==-1)$myau=0;
                        if($au["keyword"]=="admin")$myau=1;
                        if($au["keyword"]==$this->_configs["authority"]){
                            if($au["value"]==0 && $myau==-1) $myau=0;
                            if($au["value"]==1) $myau=1;
                        }
                    }
                }
                return $myau>=$this->_configs["MinAuthority"]?$myau:-1;
            }return -1;
        }
        public function index()
	{
            
            $CFregion= json_encode($this->CFregion());
            $this->smarty->assign('src', $CFregion);
            $this->smarty->assign('_SESSION', $_SESSION);
            $this->smarty->assign('configs', $this->_configs);
            $this->smarty->view('admin-planners/region/01_region','contentofview');
            $this->smarty->view('admin-planners/00_1_styles','styles');
            $this->smarty->view('admin-planners/00_2_scripts','scripts');
            $this->smarty->view('admin-planners/00_3_menus','menus');
            $this->smarty->view('admin-planners/10_template','view');
            $this->smarty->display('admin-planners/01_manage');
	}
        function js_data(){
            $CFregion= json_decode(@file_get_contents(APPPATH."HTML_DOM/CFregion.txt"));
            
            echo json_encode($CFregion);
            
        }
        function addreg(){

            if(isset($_POST["params"])){
                $params=$_POST["params"];
                //print_r($params);return;
                if(count($this->region_model->getByName($_POST["params"]["name"],$_POST["params"]["parent"]))==0){
                    if($this->region_model->insert($params)){

                    }
                }
            }
            
            echo json_encode($this->CFregion());
        }
        function ovrreg(){

            if(isset($_POST["params"])){
                $id=$_POST["id"];
                $params=$_POST["params"];
                if($this->region_model->update($id,$params)){

                }
            }
            
            echo json_encode($this->CFregion());
        }
        function delreg(){
            
            
            if(isset($_POST["id"])){
                $id=$_POST["id"];
                if($this->region_model->delete($id)){
                    
                }
            }
            echo json_encode($this->CFregion());
        }
        function CFregion(){
            $dt=$this->region_model->gets();
            $CFregion[]=array(
                "id"=>"0",
                "name"=>"Khu Vực",
                "text"=>"Khu Vực",
                "parentid"=>"-1",
                "ship"=>""
            );
            foreach ($dt as $r){
                if(!isset($r->delete)){
                    //$t="$r->name - $r->ship<sup>đ</sup>";
                    $t="$r->name";
                }else{
                    //$t="<font color='red'>$r->name - $r->ship<sup>đ</sup></font>";
                    $t="<font color='red'>$r->name</font>";
                }
                $CFregion[]=array(
                    "id"=>$r->id,
                    "name"=>$r->name,
                    "text"=>"$t",
                    "parentid"=>$r->parent,
                    "ship"=>$r->ship
                );
            }
            return $CFregion;
        }
        
       
        
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */